package admin.code.generator.common;

import java.util.HashMap;
import java.util.Map;

import admin.code.generator.util.StringUtil;
import freemarker.log.Logger;

/**
*
* @author dw_chenqi
* @date 2016年3月7日下午3:40:54
*/
public class DataTypeConverter {
	
	private static Logger log = Logger.getLogger(DataTypeConverter.class.getName());
	
	public static Map<String, String> typeMapping = new HashMap<String, String>();

	//	/https://dev.mysql.com/doc/connector-j/en/connector-j-reference-type-conversions.html
	static {
		typeMapping.put("CHAR", "String");
		typeMapping.put("VARCHAR", "String");
		typeMapping.put("LONGVARCHAR", "String");
		typeMapping.put("NUMERIC", "BigDecimal");
		typeMapping.put("DECIMAL", "BigDecimal");
		typeMapping.put("BIT", "Boolean");
		typeMapping.put("TINYINT", "Integer");
		typeMapping.put("SMALLINT", "Integer");
		typeMapping.put("INT", "Integer");
		typeMapping.put("BIGINT", "Long");
		typeMapping.put("FLOAT", "Float");
		typeMapping.put("DOUBLE", "Double");
		typeMapping.put("BINARY", "Byte[]");
		typeMapping.put("VARBINARY", "Byte[]");
		typeMapping.put("LONGVARBINARY", "Byte[]");
		typeMapping.put("DATE", "java.sql.Date");
		typeMapping.put("TIME", "java.sql.Time");
		//blob类型用得比较少,需要的时候再支持
	}

	public static String getJavaDataType(String mysqlDataType) {
		String javaDataType = typeMapping.get(mysqlDataType);
		if (!StringUtil.isNotBlank(javaDataType)) {
			log.info("没有配置"+mysqlDataType+"类型的映射关系!使用默认值String!");
			return "String"; //默认用String
		}
		return javaDataType;
	}
}
